<?php

class AdminController extends Controller
{
    public $layout = 'admin';

    public function actionLogin()
    {
        $this->layout = 'admin-login';
        $this->render('login');
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionPlayer()
    {
        $this->render('add-player');
    }

    public function actionPlayerAdd()
    {
        $this->addPlayer();
        $this->redirect('index.php?r=admin/players');
    }

    public function actionPlayerDelete()
    {
        if (isset($_GET["id"])) {
            $obj = Player::model()->findByPk($_GET["id"]);
            $obj->delete();
        }
        $this->redirect('index.php?r=admin/players');
    }

    public function actionPlayers()
    {
        $model = new Player();
        $criteria = new CDbCriteria(array(
            'order' => 'id ASC'
        ));
        $total = $model->count($criteria);
        $pages = new CPagination($total);
        $pages->pageSize = Yii::app()->params['recordsPerPage'];
        $pages->applyLimit($criteria);
        $list = $model->findAll($criteria);
        $this->render('manage-player', array(
            'list' => $list,
            'pages' => $pages
        ));
    }

    public function actionCoach()
    {
        $this->render('add-coach');
    }

    public function actionCoachAdd()
    {
        $this->addCoach();
        $this->redirect('index.php?r=admin/coaches');
    }

    public function actionCoachDelete()
    {
        if (isset($_GET["id"])) {
            $obj = Coach::model()->findByPk($_GET["id"]);
            $obj->delete();
        }
        $this->redirect('index.php?r=admin/coaches');
    }

    public function actionCoaches()
    {
        $this->render('manage-coach');
    }

    public function actionManager()
    {
        $this->render('add-manager');
    }

    public function actionManagerAdd()
    {
        $this->addManager();
        $this->redirect('index.php?r=admin/managers');
    }

    public function actionManagerDelete()
    {
        if (isset($_GET["id"])) {
            $obj = Manager::model()->findByPk($_GET["id"]);
            $obj->delete();
        }
        $this->redirect('index.php?r=admin/managers');
    }

    public function actionManagers()
    {
        $model = new Manager();
        $criteria = new CDbCriteria(array(
            'order' => 'id ASC'
        ));
        $total = $model->count($criteria);
        $pages = new CPagination($total);
        $pages->pageSize = Yii::app()->params['recordsPerPage'];
        $pages->applyLimit($criteria);
        $list = $model->findAll($criteria);
        $this->render('manage-manager', array(
            'list' => $list,
            'pages' => $pages
        ));
    }

    public function actionUser()
    {
        $this->render('add-user');
    }

    public function actionUserAdd()
    {
        $this->addUser();
        $this->redirect('index.php?r=admin/users');
    }

    public function actionUserDelete()
    {
        if (isset($_GET["id"])) {
            $obj = User::model()->findByPk($_GET["id"]);
            $obj->delete();
        }
        $this->redirect('index.php?r=admin/users');
    }

    public function actionUsers()
    {
        $model = new User;
        $criteria = new CDbCriteria(array(
            'condition' => 'admin=1',
            'order' => 'id ASC'
        ));
        $total = $model->count($criteria);
        $pages = new CPagination($total);
        $pages->pageSize = Yii::app()->params['recordsPerPage'];
        $pages->applyLimit($criteria);
        $list = $model->findAll($criteria);
        $this->render('manage-user', array(
            'list' => $list,
            'pages' => $pages
        ));
    }
}